<?php

namespace App\Http\Resources;

use App\Repositories\GroupRepository;

class GroupFull extends Group
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
		
		/** @var GroupRepository $groupRepository */
        $groupRepository = resolve(GroupRepository::class);
		
        $isCreater = $this->user_id == request()->user()->id;
		$hasJoined = (bool)$this->account;
        $data = parent::toArray($request);

        list($copper, $bet, $win, $announce) = $hasJoined ? [
            $this->account->copper,
            $this->account->join_count,
            $this->account->win_count,
            $this->account->announce_count,
        ] : array_fill(0, 4, 0);

        return array_merge($data, [
            'creater' => new User($this->creater),
            'join_type_name' => GroupRepository::$joinTypes[$this->join_type],
            'create_quiz_type' => $this->create_quiz_type,
            'join_password' => $isCreater ? $this->join_password : null,
            'copper' => $this->copper,
            'description' => $this->description,
            'created_at' => $this->created_at->format('Y-m-d H:i:s'),
            'is_creater' => $isCreater,
            'has_joined' => $hasJoined,
			'can_create_quiz' =>  $groupRepository->canCreateQuiz($this->resource, request()->user(), $hasJoined),
            'links' => array_merge($data['links'], [
                'users' => route('group_users', ['group' => $this->id]),
                'leaderboard' => route('group_users_leaderboard', ['group' => $this->id]),
                'quizzes' => route('group_quizzes', ['group' => $this->id]),
                'modify' => route('group_modify', ['group' => $this->id]),
            ]),
            'my' => [
                'copper' => $copper,
                'bet' => $bet,
                'win' => format_percentage($win, $announce),
            ]
        ]);
    }
}
